package com.all.security.model.entity;

import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author
 * @ClassName SysUserDetails
 * @Date 2024/3/31 14:56
 * @Description security userdetails
 * @Version 1.0
 */
@ToString
public class SysUserDetails implements UserDetails {

    // 用户id
    private Long userId;
    // 用户名
    private String username;
    // 用户昵称
    private String nickname;
    // 用户密码
    private String password;
    // 用户权限
    private Set<GrantedAuthority> authorities;
    // 用户是否过期
    private boolean accountNonExpired;
    // 用户是否锁定
    private boolean accountNonLocked;
    // 用户凭证是否过期(密码)
    private boolean credentialsNonExpired;
    // 用户是否启用
    private boolean enabled;

    public SysUserDetails(Long userId, String username, String nickname, String password,
                          Collection<? extends GrantedAuthority> authorities) {
        this.userId = userId;
        this.username = username;
        this.nickname = nickname;
        this.password = password;
        this.authorities = Collections.unmodifiableSet(
                authorities.stream().map(authority ->
                        new SimpleGrantedAuthority(authority.getAuthority())
                ).collect(Collectors.toSet()));
        this.accountNonExpired = true;
        this.accountNonLocked = true;
        this.credentialsNonExpired = true;
        this.enabled = true;
    }

    public SysUserDetails(Long userId, String username, String nickname, String password,
                               Collection<? extends GrantedAuthority> authorities,
                               boolean accountNonExpired, boolean accountNonLocked,
                               boolean credentialsNonExpired, boolean enabled) {
        this.userId = userId;
        this.username = username;
        this.nickname = nickname;
        this.password = password;
        this.authorities = Collections.unmodifiableSet(
                authorities.stream().map(authority ->
                        new SimpleGrantedAuthority(authority.getAuthority())
                ).collect(Collectors.toSet()));
        this.accountNonExpired = accountNonExpired;
        this.accountNonLocked = accountNonLocked;
        this.credentialsNonExpired = credentialsNonExpired;
        this.enabled = enabled;
    }

    public Long getUserId() {
        return userId;
    }

    public String getNickname() {
        return nickname;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
